package sv.com.renacempleo.dao.ctg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.renacempleo.dao.BaseDaoImpl;
import sv.com.renacempleo.dto.ctg.CtgPsiquico;
import sv.com.renacempleo.exception.WebException;

public class CtgPsiquicoDaoImpl extends BaseDaoImpl implements CtgPsiquicoDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> findAllActivesAsArray()
			throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgPsiquico.class);
		criteria.addOrder(Order.asc("ctgPsiquicoId"));
		criteria.addOrder(Order.asc("ctgPsiquicoNombre"));
		criteria.add(Restrictions.eq("ctgPsiquicoEstado", "1"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgPsiquicoId")).
				add(Projections.property("ctgPsiquicoNombre"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllAsArray() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgPsiquico.class);
		criteria.addOrder(Order.asc("ctgPsiquicoId"));
		criteria.addOrder(Order.asc("ctgPsiquicoNombre"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgPsiquicoId")).
				add(Projections.property("ctgPsiquicoCaracteristica")).
				add(Projections.property("ctgPsiquicoNombre")).
				add(Projections.property("ctgPsiquicoDescripcion")).
				add(Projections.property("ctgPsiquicoNivel")).
				add(Projections.property("ctgPsiquicoEstado"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@Override
	public void delete(Long ctgPsiquicoId) throws WebException {
		try{
			String hqlQuery = "delete from CtgPsiquico where" +
				" ctgPsiquicoId = :ctgPsiquicoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgPsiquicoId", ctgPsiquicoId);
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (Exception e) {
			throw new WebException("Problemas al tratar de eliminar el registro <br />Registro puede estar asociado");
		}	
	}

	@Override
	public void toggleActive(Long ctgPsiquicoId, String ctgPsiquicoEstado)
			throws WebException {
		try{
			String hqlQuery = "update CtgPsiquico set " +
				"ctgPsiquicoEstado = :ctgPsiquicoEstado where" +
				" ctgPsiquicoId = :ctgPsiquicoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgPsiquicoId", ctgPsiquicoId);
			parameters.put("ctgPsiquicoEstado", "1".equals(ctgPsiquicoEstado) ? "0" : "1");
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (WebException e) {
			throw e;
		} catch (Exception e) {
			throw new WebException("Problemas al actualizar la informaci\u00F3n");
		}
	}

}
